package com.example.trueordare.tools;

import android.content.Context;
import android.content.Intent;

import com.example.trueordare.model.Question;

import com.example.trueordare.R;
public class QuestionTools {
    private static Question[] questions;

    private static volatile QuestionTools instance;

    //构造函数私有化 防止外部通过new 关键字创建对象
    private QuestionTools(){
        //初始化问题数组
        questions = new Question[9];
        questions[0] = new Question(R.string.question1_text);
        questions[1] = new Question(R.string.question2_text);
        questions[2] = new Question(R.string.question3_text);
        questions[3] = new Question(R.string.question4_text);
        questions[4] = new Question(R.string.question5_text);
        questions[5] = new Question(R.string.question6_text);
        questions[6] = new Question(R.string.question7_text);
        questions[7] = new Question(R.string.question8_text);
        questions[8] = new Question(R.string.question9_text);
    }

    //获取QuestionTools实例
    public static QuestionTools getInstance() {
        //Double-Check 确保只生产一个QuestionTools实例
        if (instance == null)
            synchronized (QuestionTools.class){
                if (instance == null)
                    instance = new QuestionTools();
            }

        return instance;
    }
    //获取问题列表
    public Question[] getQuestions() {
        return questions;
    }

    //分享到QQ
    public void sendMessage(Context context){
        //1、创建StringBuffer拼接字符串
        StringBuilder sb = new StringBuilder();
        //遍历拼接
        for (int i = 0; i < questions.length; i++) {
            String str = (i + 1) + "." + context.getString(questions[i].getQuestionId()) + "\n"
                    + "答:" +  questions[i].getAnswer() + "\n";
            sb.append(str);
        }

        //使用隐式intent调起QQ
        Intent sendIntent = new Intent();
        sendIntent.setAction(Intent.ACTION_SEND);
        sendIntent.putExtra(Intent.EXTRA_TEXT, sb.toString());
        sendIntent.setType("text/plain");
        try {
            //setClassName跳转到不同应用下的Activity
            sendIntent.setClassName("com.tencent.mobileqq", "com.tencent.mobileqq.activity.JumpActivity");
            Intent chooserIntent = Intent.createChooser(sendIntent, "选择分享途径");
            if (chooserIntent == null) {
                return;
            }
            context.startActivity(chooserIntent);
        } catch (Exception e) {
            context.startActivity(sendIntent);
        }
    }

}
